﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DoAn_BumTruong_DesignPattern
{
    public partial class Detail : System.Web.UI.Page
    {
        string arID = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            arID = Request.QueryString["ArticleID"] ?? "";
            if (!IsPostBack)
            {
                LoadData();
                LoadComment();
            }
            if (User.Identity.Name != "")
            {
                plComment.Visible = true;
            }
        }
        Facades arfacade = new Facades();
        private void LoadData()
        {
            string articleID = Request.QueryString["ArticleID"] ?? "";
            //string articleID = "21";
            if (articleID != "")
            {
                RepeaterIntroText.DataSource = arfacade.ArticleByID(articleID);
                RepeaterIntroText.DataBind();
            }
        }
        private void LoadComment()
        {
            string articleID = Request.QueryString["ArticleID"] ?? "";
            if (articleID != "")
            {
                RepeaterComment.DataSource = arfacade.GetCommentByArticleID(articleID);
                RepeaterComment.DataBind();
                lbComment.Text = arfacade.CountCommentByAritcleID(articleID).ToString() + " comment";
            }
            else
            {
 
            }
        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            btComment.Click += btComment_Click;
        }
        //GetData
        private Comments GetData()
        {
            Random rd = new Random();
            int numberimage = rd.Next(0, 20);
            Comments data = new Comments();
            if (User.Identity.Name != "")
            {
                data.Client = User.Identity.Name;
            }
            data.CreateDate = DateTime.Now;
            data.Context = txtComment.Text;
            data.Avata = numberimage.ToString();
            data.ShowHide = true;
            data.ArticleID = Convert.ToInt32(arID);
            return data;
        }
        void btComment_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                bool result = false;
                Comments data = GetData();
                result = arfacade.AddComment(data);
                if (result)
                {
                    txtComment.Text = "";
                    LoadComment();
                }
            }
        }
    }
}